
class CursorControl {
  constructor(ev) {
    this.element = ev;
  }
  getType() {
    return Object.prototype.toString.call(this.element).match(/^\[object\s(.*)\]$/)[1];
  }
  getStart() {
    var start;
    if (this.element.selectionStart || this.element.selectionStart == '0') {
      start = this.element.selectionStart;
    }
    else if (window.getSelection) {
      var rng = window.getSelection().getRangeAt(0).cloneRange();
      rng.setStart(this.element, 0);
      start = rng.toString().length;
    }
    return start;
  }
  insertText(text) {
    this.element.focus();
    if (document.all) {
      var c = document.selection.createRange();
      document.selection.empty();
      c.text = text;
      c.collapse();
      c.select();
    }
    else {
      var start = this.getStart();
      if (this.getType() == 'HTMLDivElement') {
        this.element.innerHTML = this.element.innerHTML.substr(0, start) + text + this.element.innerHTML.substr(start);
      } else {
        this.element.value = this.element.value.substr(0, start) + text + this.element.value.substr(start);
      }
    }
  }
}



function fn(id, text) {
  let cursorControl = new CursorControl(document.getElementById(id));
  cursorControl.insertText(text);
}


export default fn